package com.lxq.market.servlet;

import com.google.gson.JsonObject;
import com.lxq.market.model.App;
import com.lxq.market.model.User;
import com.lxq.market.service.AppService;
import com.lxq.market.util.Defined;
import com.lxq.market.util.SessionSignIn;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.UUID;

@WebServlet("/upload/app")
@MultipartConfig
public class UploadAppServlet extends HttpServlet {

    SessionSignIn sign = SessionSignIn.getInstance();
    AppService appService=AppService.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String appName = req.getParameter("app_name");
        String appDesc = req.getParameter("app_desc");
        String appCategory=req.getParameter("app_category");
        Part appFilePart = req.getPart("app_file");
        Part appIconPart = req.getPart("app_icon");
        int shots = Integer.parseInt(req.getParameter("shots"));
        ArrayList<Part> shotList=new ArrayList<>();
        for (int i = 0; i < shots; i++) {
            shotList.add(req.getPart("appShot_"+i));
        }
        if (appFilePart == null || appName == null || appDesc == null || appIconPart == null||appCategory==null) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        appName=appName.trim();
        appDesc=appDesc.trim();
        appCategory=appCategory.trim();
        App newApp=new App();
        User user = sign.find(req.getSession().getId());
        String appSavePath=Defined.external + "/" + user.getUserAccount() + "/app/" + appName;
        JsonObject jsonObject=new JsonObject();
        if (!new File(appSavePath).mkdirs()) {
            System.out.println("创建文件夹失败");
            jsonObject.addProperty("upload_result",false);
            jsonObject.addProperty("info","创建文件夹失败");
            resp.getOutputStream().write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
            return;
        }
        if (!new File(appSavePath+"/img").mkdirs()) {
            System.out.println("创建截图文件夹失败");
            jsonObject.addProperty("upload_result",false);
            jsonObject.addProperty("info","创建截图文件夹失败");
            resp.getOutputStream().write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
            return;
        }
        String mapExternal=Defined.urlMap+"/"+user.getUserAccount()+"/app/"+appName;
        appFilePart.write(appSavePath+"/"+appName+".apk");
        appIconPart.write(appSavePath+"/"+appName+".png");
        StringBuilder shotListStr=new StringBuilder();
        int i=0;
        for (Part part : shotList) {
            part.write(appSavePath+"/img/appShot_"+i+".png");
            shotListStr.append(mapExternal).append("/img/appShot_").append(i).append(".png;");
            i++;
        }
        newApp.setAppId(UUID.randomUUID().toString());
        newApp.setAppName(appName);
        newApp.setAppDesc(appDesc);
        newApp.setAppCategory(appCategory);
        newApp.setAppFrom(user.getUserAccount());
        newApp.setAppState("上架");
        newApp.setAppSize(appFilePart.getSize()/1000);
        newApp.setAppFile(mapExternal+"/"+appName+".apk");
        newApp.setAppLogo(mapExternal+"/"+appName+".png");
        newApp.setAppShot(shotListStr.toString());
        appService.addApp(newApp);
        jsonObject.addProperty("upload_result",true);
        jsonObject.addProperty("info","上传成功");
        resp.getOutputStream().write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
    }
}
